<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;

class ArticleController extends BaseController {

    /**
     * 列表
     */
    public function lists(){
        $keyword = I('keyword','','trim');      //过滤开头结尾空格
        if($keyword){
            $map['title'] = array('like',"%$keyword%");//模糊查询
            $this->assign('keyword',$keyword);  //输出数据
        }
        $article = M('article');
        $count  = $article->count();         //article表总数据量
        $row    = 10;                           //每页显示条数
        $Page   = new \Think\Page($count,$row); //实例化分页类
        $show   = $Page->show();                //调用show方法
        $datalists = $article->where($map)->order('sort desc')
            ->limit($Page->firstRow.','.$Page->listRows) //结合limit方法
            ->select();
        $this->assign('page',$show);    //输出分页数据
        $title  = '文章列表管理';
        $this->assign('title',$title);
        $this->assign('datalists',$datalists);
        $this->display();
    }

    /**
     * 新增或者编辑
     */
    public function add(){
        $article  = M('article');
        $category = M('category');  //遍历文章类别
        $category = $category->select();
        $tag      = M('tag');
        $tag      = $tag->select();
        $at       = D('ArticleTag');
        $this->assign('list',$category);
        $this->assign('tag',$tag);
        if(IS_POST){
            $data = I('post.');
            if($data['id']){    //编辑
                $data['update_time'] = time();
                $res  = $article->where(array('id'=>$data['id']))->save($data);
                $at   = $at->addData($data['id'],$data['tid']);
            }else{              //新增
                $data['create_time'] = time();
                $res  = $article->add($data);
                $lid = $article->getLastInsID();
                $at   = $at->addData($lid,$data['tid']);
            }
            if($res !== false){
                $this->success('操作成功',U('lists'));  //跳转到列表页
            }else{
                $this->error('添加失败');
            }
        }else{
            /**id存在是编辑的情况，需要获取文章**/
            $map['id']  = $id = I('get.id');
            if($id){
                $article = $article->where($map)->select(); //查找文章
                $tid     = M('ArticleTag')->where(array('aid'=>$id))->getField('tid',true);
                $tids   = implode(",", $tid);
                $this->assign('get_id',$id);
                $this->assign('tids',$tids);
                $this->assign('data',$article);
            }
            $this->display();
        }
    }

    /**
     * 单选删除
     */
    public function delete(){
        $map['id'] = I('id',0,'int');
        $res = M('article')->where($map)->delete();
        if($res){
            $message = '删除成功';
        }else{
            $message = '删除失败';
        }
        $this->ajaxReturn($message);
    }

    /**
     * 全选删除
     */
    public function delAll(){
        $ids = I('ids',0);
        //删除所有选中高级分类
        $map['id']  = array('in',$ids);
        if(M('article')->where($map)->delete()){
            $message = '删除成功';
        }else {
            $message = '删除失败';
        }
        $this->ajaxReturn($message);
    }


}